package com.example.index.system

import android.os.Bundle
import android.util.Log
import com.google.android.material.snackbar.Snackbar
import android.view.Menu
import android.view.MenuItem
import com.cat.annotation.Index
import com.example.index.BaseActivity
import com.example.index.R
import kotlinx.android.synthetic.main.activity_scrolling.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


@Index(keywords = "Ui/动效", link = "")
class ScrollingActivity : BaseActivity() {

    val TAG = "ScrollingActivity"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scrolling)
        setSupportActionBar(toolbar)
        fab.setOnClickListener { view ->
            Snackbar.make(view, getString(R.string.replaceaction), Snackbar.LENGTH_LONG)
                .setAction("Action") {

                }.show()
        }
        EventBus.getDefault().register(this)

    }


    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_lang, menu)
        return super.onCreateOptionsMenu(menu)
    }


    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {

            R.id.lang_cn -> {
                if (LocaleManager.setLocale(
                        this,
                        LocaleManager.cnLocale
                    )
                )
                    LocaleManager.postBroadCast()
            }

            R.id.lang_en -> {
                if (LocaleManager.setLocale(
                        this,
                        LocaleManager.enLocale
                    )
                )
                    LocaleManager.postBroadCast()
            }

        }
        return super.onOptionsItemSelected(item)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onConfigurationNeedUpdate(event: UpdateLocaleEvent) {
        recreate()
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }


    override fun onSaveInstanceState(outState: Bundle) {
        Log.i(TAG, "onSaveInstanceState: ")
        super.onSaveInstanceState(outState)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        super.onRestoreInstanceState(savedInstanceState)
    }

}
